package com.chenwut.threadDemo.exercise_1;

/**
 * @author ChenPi
 * @since 2024/09/19 16:11
 */
public class MyRunable02 implements Runnable {

    static int ticket = 0;

    @Override
    public void run() {
        while (true) {
            synchronized (this) {
                if (saleTicket()) {
                    break;
                }
            }
        }
    }

    private static boolean saleTicket() {
        ticket++;
        if (ticket > 100) {
            return true;
        }
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println(Thread.currentThread().getName() + " 发放了 票：" + ticket + " 剩余票数为： " + (100 - ticket));
        return false;
    }
}
